<?php
class UsersController extends AppController {

	var $name = 'Users';
        public function beforeFilter() {
            parent::beforeFilter();
            $this->Auth->allow(array('*'));
               //pr($this->Auth->user()); 
            //$this->Auth->allowedActions = array('login', 'logout', 'register', 'index', 'checkUsername');
        }
        
	function index() {
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('user', $this->User->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->User->create();
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
		$groups = $this->User->Group->find('list');
		$this->set(compact('groups'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->User->read(null, $id);
		}
		$groups = $this->User->Group->find('list');
		$this->set(compact('groups'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for user', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->User->delete($id)) {
			$this->Session->setFlash(__('User deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('User was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
        
        public function login() {
            if ($this->Session->read('Auth.User')) {
                $this->Session->setFlash('You are logged in!');
                $this->redirect('/', null, false);
            }
        }
        public function logout() {
            $this->Session->setFlash('Good-Bye');
			$this->redirect($this->Auth->logout());

        }
        
    function initDB() {
        $group = & $this->User->Group;
        //Allow admins to everything
        $group->id = 4;
        $this->Acl->allow($group, 'controllers');
        
        //allow Moderator to posts and widgets
        $group->id = 5;
        $this->Acl->deny($group, 'controllers');
        $this->Acl->allow($group, 'controllers/Questions');
        $this->Acl->allow($group, 'controllers/Answers');
	$this->Acl->allow($group, 'controllers/Tags'); 
        
        $this->Acl->allow($group, 'controllers/Tags/index'); 
		
		
        //allow users to only add and edit on posts and widgets
        /*$group->id = 3;
        $this->Acl->deny($group, 'controllers');
        $this->Acl->allow($group, 'controllers/Posts/add');
        $this->Acl->allow($group, 'controllers/Posts/edit');
        $this->Acl->allow($group, 'controllers/Widgets/add');
        $this->Acl->allow($group, 'controllers/Widgets/edit');*/
    }
    
        public function register() {
           
            if (!empty($this->data)) {
                //pr($this->data); die();
                if ( $this->data['User']['password'] ==  $this->Auth->password($this->data['User']['cpwd'])) {
                    $this->data['User']['group_id'] = $this->User->Group->getGroupDefaltId(DEFAULT_USER_GROUP);
                    $this->User->create();
                    
                    if ($this->User->save($this->data)) {
                        $this->Session->setFlash(__('Your register is completed', true));
                        $this->redirect('/users/index');
                    } else {
                        $this->Session->setFlash(__('Can not register.', true));
                    }
                } else {
                     $this->Session->setFlash('Typed passwords did not match');
                     //$this->register('/users/register');
                }
                
            }
            
            $this->set('register', __('Register Form', true));
            
        }
        
        
        public function checkUsername($username) {
            $this->ajaxRequestLayout();
            
            //$exist = $this->User->checkUsername($username);
            $this->data['User']['username'] = $username;
            $this->User->set($this->data);
            $errors_msg = $this->User->invalidFields();
            if (sizeof($errors_msg) > 0) {
                $result = array (
                        'exist' =>  1,
                        'message' => $errors_msg['username']
                    );
            } else {
                 $result = array (
                        'exist' => 0,
                        'message' => 'Avalable'
                    );
            }
            $this->set('result', $result);
        }
        
        public function checkEmail($email) {
            $this->ajaxRequestLayout();
            $this->data['User']['email'] = $email;

            $this->User->set($this->data);
            $errors_msg = $this->User->invalidFields();
            if (sizeof($errors_msg) > 0) {
                $result = array (
                        'exist' =>  1,
                        'message' => $errors_msg['email']
                    );
            } else {
                 $result = array (
                        'exist' => 0,
                        'message' => 'Avalable'
                    );
            }
            $this->set('result', $result);
        }

}
